package com.jchy.maze;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MazeAppGui extends JFrame
{
	private static final long serialVersionUID = 999;
	
	public JPanel panel;
	
	public MazeAppLabel screen;

    public MazeAppGui()
    {
        setTitle("Maze Application");
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        screen = new MazeAppLabel();
        panel.add(screen, BorderLayout.CENTER);
        getContentPane().add(panel);

		addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		buildMenu(screen);
		pack();

    }
    
	private void buildMenu(MazeAppLabel inScreen)
	{
		// Instantiates JMenuBar, JMenu,
		// and JMenuItem.
		JMenuBar mb = new JMenuBar();
		JMenu filemnu = new JMenu("File");
		JMenuItem runitm = new JMenuItem("Run");
		JMenuItem exititm = new JMenuItem("Exit");
		//Closes the application from the Exit 
		//menu item.
		exititm.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
             
		});// Ends method

        runitm.addActionListener(new RunActionListener(screen));
 
		//Adds the item to the menu object
		filemnu.add(runitm);
		filemnu.add(exititm);
		//Adds the menu object with item
		//onto the menu bar
		mb.add(filemnu); 
		//Sets the menu bar in the frame
		setJMenuBar(mb);
	}

    public static void main(String[] args)
    {
        MazeAppGui frame = new MazeAppGui();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(684,684);
        frame.setVisible(true);
    }

}

class RunActionListener implements ActionListener {
	
	private MazeAppLabel map;
	
	public RunActionListener(MazeAppLabel inMap)
	{
		map = inMap;
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		map.newMaze();
		map.repaint();
	}
	
}

